---[[
--
--
--  取出redis中的dellist id
--  根据id 查询redis value
--  删除redis key
--  清空mongodb中的gfile字段
--  删除storage
--]]
local shell = require("resty.shell")
local utils                    = require "gcs.utils"
local cjson                    = require "cjson.safe"
local red_method               = require "gcs.list.method"
local mongo_method             = require "gcs.mongodb.method"
local storage                  = require "resty.gcsdfs.storage"
local up_utils                 = require "gcs.upload.upload_utils"
local config_table             = ngx.shared.gcs_config   --声明ngx.shared.gcs_config
local store_connect_timeout    = config_table:get("p_store_connect_timeout")
local store_keep_timeout       = config_table:get("p_store_keep_timeout")
local store_keep_count         = config_table:get("p_store_keep_count")


local table_name = "gcxx1"

local tb_value = {
  timeout = store_connect_timeout,
  keep_timeout = store_keep_timeout,
  keep_count = store_keep_count
}
-----------查询2天之前的日期
local status, out, err = shell.execute("date -d '2 day ago' +'%Y-%m-%d'")
if not status then
  utils.gcs_log("error","resty_shell err")
end
local a = 2014-12-18--string.gsub(out,'%c','')
local list_name = a.."-dellist"

--local utils = require "gcs.utils"
local num = 3000 --一次删除的次数
for i=1,num do
  local id,err = red_method.pop("r",list_name)
  --update_data(table_name,update_data,selector)
  if id then
    -----先取出redis中的json
    local tb_key = {}
    tb_key[1] = id
    local tb_keydata,err = red_method.get_data(tb_key)
    if tb_keydata then
      local res,err = red_method.del_data(tb_key)                 ----删除key
      if not res then
      utils.gcs_log("error","删除redis key成功")
        red_method.push("r",list_name,id)         ---插回dellist
      else
        
        local cjson_data = tb_keydata[id]
        local tb_data = cjson.decode(cjson_data)
        if tb_data then
            if tb_data.gfile1 then
              local str_res1 = cjson.decode(tb_data.str_res1)
              local storage_tb = {}
              storage_tb.gfile1 = tb_data.gfile1
              
              local res,err = up_utils.del_storage(str_res1,tb_value,storage_tb)
              if not res then
                utils.gcs_log("error","删除gfile1失败")
              end
            end
            if tb_data.gfile2 then
              local str_res2 = cjson.decode(tb_data.str_res2)
              local storage_tb = {}
              storage_tb.gfile1 = tb_data.gfile2
              local res,err = up_utils.del_storage(str_res2,tb_value,storage_tb)
              if not res then
                utils.gcs_log("error","删除gfile2失败")
              end
            end
            if tb_data.gfile3 then
              local str_res3 = cjson.decode(tb_data.str_res3)
              local storage_tb = {}
              storage_tb.gfile1 = tb_data.gfile3
              local res,err = up_utils.del_storage(str_res3,tb_value,storage_tb)
              if not res then
                utils.gcs_log("error","删除gfile3失败")
              end
            end
             ----删除mongodb
 --          local n,err = mongo_method.del_data(table_name,{id = id})    --删除mogodb
  --         if not n then
    --        utils.gcs_log("error","删除mongodb失败")
     --      end
          end
        end
      end
    end
  end    
  
  
